/*
 * parser.cpp
 *
 *  Created on: 2011-10-17
 *      Author: mateusz
 */
#include "parser.h"

//using namespace std;

Parser::Parser() {
	fileOpened = false;
}

Parser::~Parser() {
	if (fileOpened) {
		closeFile();
	}
}

bool Parser::openFile(string fileName) {
	if (!fileLink.is_open()) {
		// open file to read
		fileLink.open(fileName.c_str(), ios::in);

		// check if file is open
		if (fileLink.is_open()) {
			fileOpened = true;
			return false;
		}
	}
	return true;
}

void Parser::closeFile() {
	if (fileLink.is_open()) {
		fileLink.close();
	}
}

bool Parser::loadData(string fileName, int maxTasks, char commentDelimiter) {
	string line;
	int jobId, jobSubmitTime, jobRunTime, jobProcessorsNumber, tmp;
	istringstream iss; // input string stream to read from

	// open file if not opened yet
	if (!this->fileOpened) {
		if (this->openFile(fileName)) {
			return false;
		}
	}

	while (!this->fileLink.eof()) {
		getline(this->fileLink, line);

		if (line.length() > 0) {
			// check for comment beggining (default with ;)
			if (line[0] != commentDelimiter) {
				//cout << line << endl;

				// make stream from string
				iss.str(line);

				// get values from line
				iss >> jobId >> jobSubmitTime >> tmp >> jobRunTime >> jobProcessorsNumber;
				cout << "id:" << jobId << "; submit time: " << jobSubmitTime << "; run time: " << jobRunTime
						<< "; processors: " << jobProcessorsNumber << endl;

				// TO DO - save values to jobs instance
			} else {
				// TO DO - check for max processors number

			}
		}
	}
	return true;
}
